package cn.gson.crmdemo.controller.privilege;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.pojo.Role;
import cn.gson.crmdemo.model.service.PrivilegeService;
import cn.gson.crmdemo.model.vo.FunctionFunTypeVo;
import cn.gson.crmdemo.model.vo.FunctionVo;
import cn.gson.crmdemo.model.vo.RoleEmpVo;

@Controller
public class RoleController {
	@Autowired
	PrivilegeService ps;

	/**
	 * 查询所有角色	
	 * @return
	 */
	@RequestMapping("role-All")
	@ResponseBody
	public Map<String, Object> selectRoleAll(Integer page,Integer pageSize,String roleName) {
		Page<Object> pages = PageHelper.startPage(page,pageSize);
		JSONArray list = ps.selectRoleAll(roleName);
		long total =  pages.getTotal();
		Map<String , Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}
	@RequestMapping("role-Alls")
	@ResponseBody
	public JSONArray selectRoleAlls() {
		return ps.selectRoleAlls();
	}
	/**
	 * 给角色授权
	 * @return
	 */
	@RequestMapping("add-role")
	public String addRole(@RequestBody List<RoleEmpVo> role) {
		ps.addRole(role);
		return "redirect:emp-All?page=1&pageSize=7&empName=";
	}
	/**
	 * 新增角色
	 * @return
	 */
	@RequestMapping("add-roles")
	public String addRoles(Role role) {
		ps.addRoles(role);
		return "redirect:role-All?page=1&pageSize=7&roleName=";
	}
	/**
	 * 编辑角色
	 * @return
	 */
	@RequestMapping("update-role")
	public String updateRole(Role roles) {
		ps.updateRoles(roles);
		return "redirect:role-All?page=1&pageSize=7&roleName=";
	}
	/**
	 * 查询功能
	 * @return
	 */
	@RequestMapping("select-function")
	@ResponseBody
	public List<FunctionVo> selectFunction(){
		return ps.selectFunction();
	}
	/**
	 * 给角色授权功能
	 */
	@RequestMapping("add-function")
	public String addFunction(@RequestBody List<FunctionFunTypeVo> functions) {
		ps.addFunction(functions);
		return "redirect:role-All";
	}
	/**
	 * 查询某个角色所有功能编号
	 * @return
	 */
	@RequestMapping("function-id")
	@ResponseBody
	public List<Integer> selectFunctionId(Integer roleId){
		return ps.selectFunctionId(roleId);
	}
	/**
	 * 删除角色
	 */
	@RequestMapping("delete-role")
	public String deleteRole(Integer roleId) {
		ps.deleteRole(roleId);
		return "redirect:role-All?page=1&pageSize=7&roleName=";
	}
}
